package cn.iocoder.yudao.module.blog.controller.admin.posteruser;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.blog.controller.admin.posteruser.vo.*;
import cn.iocoder.yudao.module.blog.dal.dataobject.posteruser.PosterUserDO;
import cn.iocoder.yudao.module.blog.convert.posteruser.PosterUserConvert;
import cn.iocoder.yudao.module.blog.service.posteruser.PosterUserService;

@Tag(name = "管理后台 - 用户海报信息")
@RestController
@RequestMapping("/blog/poster-user")
@Validated
public class PosterUserController {

    @Resource
    private PosterUserService posterUserService;

    @PostMapping("/create")
    @Operation(summary = "创建用户海报信息")
    @PreAuthorize("@ss.hasPermission('blog:poster-user:create')")
    public CommonResult<Long> createPosterUser(@Valid @RequestBody PosterUserCreateReqVO createReqVO) {
        return success(posterUserService.createPosterUser(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户海报信息")
    @PreAuthorize("@ss.hasPermission('blog:poster-user:update')")
    public CommonResult<Boolean> updatePosterUser(@Valid @RequestBody PosterUserUpdateReqVO updateReqVO) {
        posterUserService.updatePosterUser(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户海报信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('blog:poster-user:delete')")
    public CommonResult<Boolean> deletePosterUser(@RequestParam("id") Long id) {
        posterUserService.deletePosterUser(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户海报信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('blog:poster-user:query')")
    public CommonResult<PosterUserRespVO> getPosterUser(@RequestParam("id") Long id) {
        PosterUserDO posterUser = posterUserService.getPosterUser(id);
        return success(PosterUserConvert.INSTANCE.convert(posterUser));
    }

    @GetMapping("/list")
    @Operation(summary = "获得用户海报信息列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('blog:poster-user:query')")
    public CommonResult<List<PosterUserRespVO>> getPosterUserList(@RequestParam("ids") Collection<Long> ids) {
        List<PosterUserDO> list = posterUserService.getPosterUserList(ids);
        return success(PosterUserConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户海报信息分页")
    @PreAuthorize("@ss.hasPermission('blog:poster-user:query')")
    public CommonResult<PageResult<PosterUserRespVO>> getPosterUserPage(@Valid PosterUserPageReqVO pageVO) {
        PageResult<PosterUserDO> pageResult = posterUserService.getPosterUserPage(pageVO);
        return success(PosterUserConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户海报信息 Excel")
    @PreAuthorize("@ss.hasPermission('blog:poster-user:export')")
    @OperateLog(type = EXPORT)
    public void exportPosterUserExcel(@Valid PosterUserExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<PosterUserDO> list = posterUserService.getPosterUserList(exportReqVO);
        // 导出 Excel
        List<PosterUserExcelVO> datas = PosterUserConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "用户海报信息.xls", "数据", PosterUserExcelVO.class, datas);
    }

}
